/**
 * mask - jQuery xui
 *
 * Licensed under the Apache v2
 *
 * Copyright 2015 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {
    //设置大小
    function setSize(target) {
        var state = $.data(target, 'mask');
        if (state) {
            var opts = state.options;
            var mask = state.mask;
            var maskMsg = state.maskMsg;
            var cc = $(target);

            var css = {
                left: 0,
                top: 0,
                width: cc.outerWidth(),
                height: cc.outerHeight()
            };

            if (cc[0].tagName == 'BODY') {
                $.extend(css, getPageArea());
            }

            mask.css(css);
            maskMsg.css({
                left: (mask.outerWidth() - maskMsg.outerWidth()) / 2,
                top: (mask.outerHeight() - maskMsg.outerHeight()) / 2
            });
        }
    }

   	//构建遮罩层
    function init(target) {
        var state = $.data(target, 'mask');
        var opts = state.options;
        var cc = $(target).css('zoom', '1'); //ie6 bug

        var mask = $('<div class="x-mask"></div>').css('display', 'block').appendTo(cc);
        var maskMsg = $('<div class="x-mask-msg"></div>').html(opts.msg).css('display', 'block').appendTo(cc);

        state.mask = mask;
        state.maskMsg = maskMsg;
    }

    //绑定事件
    function bindEvents(target) {
        $(window).resize(function() {
            setSize(target); //resize时mask可能已经destroy了
        });
    }

    //显示遮罩层
    function show(target) {
        var state = $.data(target, 'mask');
        var mask = state.mask;
        var maskMsg = state.maskMsg;
        $('select:not([disabled])', target).addClass('x-mask-disabled-select').attr('disabled', 'disabled'); //ie6下的select的z-index bug
        mask.css('display', 'block');
        maskMsg.css('display', 'block');
        setSize(target);
        state.showCount++;
    }

    //隐藏遮罩层
    function hide(target) {
        var state = $.data(target, 'mask');
        state.showCount--;
       	state.showCount = Math.max(state.showCount, 0); //最小为0
        if (state.showCount <= 0) {
            var mask = state.mask;
            var maskMsg = state.maskMsg;
            $('select.x-mask-disabled-select').removeAttr('disabled');
            mask.css('display', 'none');
            maskMsg.css('display', 'none');
        }
    }

    //销毁遮罩层
    function destroy(target) {
        var state = $.data(target, 'mask');
        var mask = state.mask;
        var maskMsg = state.maskMsg;
        $('select.x-mask-disabled-select').removeClass('x-mask-disabled-select').removeAttr('disabled');
        mask.remove();
        maskMsg.remove();
        $(target).removeData('mask');
    }

    function getPageArea() {
        return {
            width: Math.max(document.body.clientWidth, document.documentElement.clientWidth),
            height: Math.max(document.body.clientHeight, document.documentElement.clientHeight)
        }
    }

    //遮罩层
    $.fn.mask = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.mask.methods[options](this, param);
        }
        options = options || {};

        return this.each(function() {
            var state = $.data(this, 'mask');
            var opts;
            if (state) {
                opts = $.extend(state.options, options);
            } else {
                opts = $.extend({}, $.fn.mask.defaults, options);
                $.data(this, 'mask', {
                    showCount: 0,
                    options: opts
                });

                init(this);
            }

            if (opts.hide) {
                hide(this);
            } else {
                show(this);
            }

			setSize(this);
            bindEvents(this);
        });
    };

    $.fn.mask.methods = {
        show: function(jq) {
            jq.each(function() {
                show(this);
            });
        },
        hide: function(jq) {
            jq.each(function() {
                hide(this);
            });
        },
        destroy: function(jq) {
            jq.each(function() {
                destroy(this);
            });
        }
    }

    $.fn.mask.defaults = {
        msg: '处理中，请稍候',
        hide: false
    }
})(jQuery);
